//
//  YCAlertConfig.m
//  YCAlertViewDemo
//
//  Created by carpenter on 2019/11/7.
//  Copyright © 2019 carpenter. All rights reserved.
//

#import "YCAlertConfig.h"


@implementation YCAlertConfig

- (instancetype)init{
    
    if (self = [super init]) {
        
        [self basicConfig];
    }
    return self;
}

-(void)basicConfig{
    
    self.touchHide = NO;
    
    self.maskAlpha = 0.4;
   
    self.cornersRadius = 10.0;
    
    self.animationDuration = 0.20;
    
    
    
    self.titleFont = [UIFont boldSystemFontOfSize:18];
       
    self.messageFont = [UIFont systemFontOfSize:15];
    
    self.actionTextFont = [UIFont systemFontOfSize:16];
    
    
    
    
    self.titleColor = [UIColor alert_colorWithHex:@"#000000"];
    
    self.messageColor = [UIColor alert_colorWithHex:@"#000000"];
    
    self.actionTextColor = [UIColor alert_colorWithHex:@"#000000"];
    
    self.actionBackgroundNormalColor = [UIColor alert_colorWithHex:@"#FFFFFF"];
    
    self.actionBackgroundHeightColor = [UIColor alert_colorWithHex:@"#F4F4F4"];
    
}

@end


@implementation UIColor (YCAlert)


+ (UIColor *)alert_colorWithHex:(NSString *)hex{
    
    return [self alert_colorWithHex:hex alpha:1];
}

+ (UIColor *)alert_colorWithHex:(NSString *)hex alpha:(CGFloat)alpha{
    
    //删除字符串中的空格
    NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    // strip 0X if it appears
    //如果是0x开头的，那么截取字符串，字符串从索引为2的位置开始，一直到末尾
    if ([cString hasPrefix:@"0X"]) {
        cString = [cString substringFromIndex:2];
    }
    //如果是#开头的，那么截取字符串，字符串从索引为1的位置开始，一直到末尾
    if ([cString hasPrefix:@"#"]) {
        cString = [cString substringFromIndex:1];
    }
    if ([cString length] != 6) {
        return [UIColor clearColor];
    }
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length   = 2;
    //r
    NSString *rString = [cString substringWithRange:range];
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:alpha];
}


@end

@implementation UIImage (YCAlert)

+ (UIImage *)alert_imageWithColor:(UIColor *)color{
    
    return [self alert_imageWithColor:color size:CGSizeMake(1, 1) radius:0 borderColor:nil borderWidth:0];
}

+ (UIImage *)alert_imageWithColor:(UIColor *)color
                          size:(CGSize)size
                        radius:(CGFloat)radius
                   borderColor:(UIColor *)borderColor
                   borderWidth:(CGFloat)borderWidth{
    
    UIGraphicsBeginImageContextWithOptions(size, radius == 0, [UIScreen mainScreen].scale);
    
    borderColor = borderColor?:[UIColor clearColor];
    
    CGRect targetRect = (CGRect){0, 0, size.width, size.height};
    UIImage *finalImage = nil;
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    
    if (radius == 0) {
        
        if (borderWidth > 0) {
            
            CGContextSetStrokeColorWithColor(context, borderColor.CGColor);
            CGContextSetLineWidth(context, borderWidth);
            CGContextFillRect(context, targetRect);
            
            targetRect = CGRectMake(borderWidth / 2, borderWidth / 2, size.width - borderWidth, size.height - borderWidth);
            CGContextStrokeRect(context, targetRect);
            
        } else {
            
            CGContextFillRect(context, targetRect);
        }
    } else {
        
        targetRect = CGRectMake(borderWidth / 2, borderWidth / 2, size.width - borderWidth, size.height - borderWidth);
        
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:targetRect
                                                   byRoundingCorners:UIRectCornerAllCorners
                                                         cornerRadii:CGSizeMake(radius, radius)];
        
        CGContextAddPath(UIGraphicsGetCurrentContext(), path.CGPath);
        
        if (borderWidth > 0 && borderColor) {
            
            CGContextSetStrokeColorWithColor(context, borderColor.CGColor);
            CGContextSetLineWidth(context, borderWidth);
            CGContextDrawPath(context, kCGPathFillStroke);
            
        } else {
            
            CGContextDrawPath(context, kCGPathFill);
        }
    }
    
    finalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return finalImage;
    
}

@end
